package com.pig4cloud.pig.module.controller.platform;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.platform.PlatformVideo;
import com.pig4cloud.pig.module.api.entity.product.Product;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.platform.IPlatformVideoService;
import com.pig4cloud.pig.module.service.product.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 视频列Controller
 *
 * @author ruoyi
 * @date 2022-11-09
 */
@Validated
@Api(value = "视频列控制器", tags = {"视频列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/platform/video")
public class PlatformVideoController {

    private final IPlatformVideoService platformVideoService;
    private final IMerService merService;
    private final IProductService productService;
    @Value("${ruoyi.imgUrl}")
    private String imgUrl;
    /**
     * 查询视频列列表
     */
    @ApiOperation("查询视频列列表")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:list')")
    @GetMapping("/list")
    public TableDataInfo<PlatformVideo> list(@Validated PlatformVideo platformVideo) {
        QueryWrapper<PlatformVideo> qw = new QueryWrapper<>();
        if (platformVideo.getVideoType()!=null){
            qw.eq("video_type",platformVideo.getVideoType());
        }
        TableDataInfo<PlatformVideo> td = PageUtils.buildDataInfo(platformVideoService.page(PageUtils.buildPage(),qw));
        List<PlatformVideo> list = td.getData();
        for(PlatformVideo list1:list){
            if (list1.getMerId()!=null){
                Mer mer=merService.getById(list1.getMerId());
                list1.setMerName(mer.getShopName());
            }else {
                list1.setMerName("跃豆平台");
            }
            if(list1.getProductId()!=null){
                Product product=productService.findById(list1.getProductId());
                list1.setProductName(product.getStoreName());
            }else {
                list1.setProductName("跃豆功能");
            }
            if (list1.getVideoMaskUrl()!=null){
                list1.setVideoMaskPath(imgUrl+list1.getVideoMaskUrl());
            }
            if (list1.getVideoUrl()!=null){
                list1.setVideoPath(imgUrl+list1.getVideoUrl());
            }
        }
        return td;
    }

    /**
     * 导出视频列列表
     */
    @ApiOperation("导出视频列列表")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:export')")
    @SysLog("视频列")
    @GetMapping("/export")
    public List export(@Validated PlatformVideo platformVideo) {
        List<PlatformVideo> list = platformVideoService.list();
        // ExcelUtil<PlatformVideo> util = new // ExcelUtil<PlatformVideo>(PlatformVideo.class);
        return  list;// util.exportExcel(list, "视频列");
    }

    /**
     * 获取视频列详细信息
     */
    @ApiOperation("获取视频列详细信息")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:query')")
    @GetMapping("/{id}")
    public R<PlatformVideo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        PlatformVideo platformVideo = platformVideoService.getById(id);
        return R.ok(platformVideo);
    }

    /**
     * 新增视频列
     */
    @ApiOperation("新增视频列")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:add')")
    @SysLog("视频列")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody PlatformVideo platformVideo) {
        return R.okByRow(platformVideoService.save(platformVideo) ? 1 : 0);
    }

    /**
     * 修改视频列
     */
    @ApiOperation("修改视频列")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:edit')")
    @SysLog("视频列")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody PlatformVideo platformVideo) {
        return R.okByRow(platformVideoService.updateById(platformVideo) ? 1 : 0);
    }

    /**
     * 删除视频列
     */
    @ApiOperation("删除视频列")
    @PreAuthorize("@pms.hasPermission('module:platformVideo:remove')")
    @SysLog("视频列")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(platformVideoService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
